<template>
  <div class="ki-table">
    <div class="header">
      <h2 class="name">{{listName}}</h2>
      <div class="btns">
        <slot name="btn"></slot>
      </div>
    </div>
    <div class="table">
      <el-table :data="tableData" stripe style="width: 100%" border v-bind="childrenProps">
        <template v-if="isShowSelection">
          <el-table-column type="selection" align="center"></el-table-column>
        </template>
        <template v-if="isShowIndex">
          <el-table-column type="index" label="序号" width="80px" align="center"/>
        </template>
        <el-table-column  
          v-for="item in propList" 
          :key="item.prop"
          v-bind="item"
          align="center"
          show-overflow-tooltip
        >
          <template #default="scope">
            <slot :name="item.slotName" :item="scope.row">
              {{scope.row[`${item.prop}`]}}
            </slot>  
          </template>      
        </el-table-column>
      </el-table>
    </div>
    <div class="footer">
      <slot name="footer">
        <el-pagination
          layout="prev, pager, next" 
          :total="totalCount || 0" 
          hide-on-single-page
          @current-change="handlePageChange"
          v-if="isShowFooter"></el-pagination>
      </slot>
    </div>
  </div>
</template>

<script setup lang="ts">
import { PropType } from 'vue';
import {IPropItem} from './type'
const props = defineProps({
  propList:{
    type:Array as PropType<IPropItem[]>,
    required:true
  },
  listName:{
    type:String,
    required:true
  },
  isShowIndex:{
    type:Boolean,
    default:true
  },
  isShowSelection:{
    type:Boolean,
    default:true
  },
  isShowFooter:{
    type:Boolean,
    default:true
  },
  tableData:{
    type:Array,
    required:true
  },
  totalCount:{
    type:Number,
    default:0
  },
  childrenProps:{
    type:Object,
    default:() => ({})
  },
  page:{
    type:Object,
    default:() => ({currentPage:1,pageSize:10})
  }
})
const emits = defineEmits(['update:page'])
const handlePageChange = (currentPage:number) => {
  emits('update:page',{...props.page,currentPage})
}
</script>

<style scoped lang="less">
.ki-table{
  .header{
    display: flex;
    justify-content: space-between;
    align-items: center;
    .name {
      margin-left:20px;
    }
    .btns{
      margin-right:20px;
    }
  }
  .footer{
   .el-pagination{
     justify-content: center;
     padding:40px 0;
   }
  }
}
</style>